﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BIZ
{
    public enum PathType
    {
        CategoryPath = 0,
        ArticlePath = 1,
        ProductPath = 2,
        ServicePath = 3,
        PhysicalPath = 4,
        CategoryServicePath = 5
    }

    public class UrlBIZ : BaseClass
    {
        public static Dictionary<string, PathType> listUrl = new Dictionary<string, PathType>();

        public static PathType CheckPathExist(string path)
        {
            path = path.ToLower();
            bool b = listUrl.ContainsKey(path);

            if (!b)
            {
                return PathType.PhysicalPath;
            }

            return listUrl[path];
        }

        public static void GetAllUrl()
        {
            Dictionary<string, PathType> listUrl = new Dictionary<string, PathType>();
            SinhNhatDBDataContext db = new SinhNhatDBDataContext();

            var categoryURL = from c in db.CATEGORies select c.Url.ToLower();
            foreach (var item in categoryURL)
            {
                listUrl.Add(item, PathType.CategoryPath);
            }

            var categoryServiceURL = from c in db.CategoryServices select c.Url.ToLower();
            foreach (var item in categoryServiceURL)
            {
                listUrl.Add(item, PathType.CategoryServicePath);
            }

            var articleURL = from c in db.ARTICLEs select c.Url.ToLower();
            foreach (var item in articleURL)
            {
                listUrl.Add(item, PathType.ArticlePath);
            }

            var productURL = from c in db.PRODUCTs select c.Url.ToLower();
            foreach (var item in productURL)
            {
                listUrl.Add(item, PathType.ProductPath);
            }

            var serviceURL = from c in db.SERVICEs select c.Url.ToLower();
            foreach (var item in serviceURL)
            {
                listUrl.Add(item, PathType.ServicePath);
            }

            UrlBIZ.listUrl = listUrl;
        }

        public Guid GetCategoryID(string path)
        {
            var id = (from c in DB.CATEGORies where c.Url.Equals(path) select c.CategoryID).SingleOrDefault();
            return id;
        }

        public Guid GetArticleID(string path)
        {
            var id = (from c in DB.ARTICLEs where c.Url.Equals(path) select c.ArticleID).Single();
            return id;
        }

        public Guid GetProductID(string path)
        {
            var id = (from c in DB.PRODUCTs where c.Url.Equals(path) select c.ProductID).Single();
            return id;
        }

        public Guid GetServiceID(string path)
        {
            var id = (from c in DB.SERVICEs where c.Url.Equals(path) select c.ServiceID).Single();
            return id;
        }

        public Guid GetCategoryServiceID(string path)
        {
            var id = (from c in DB.CategoryServices where c.Url.Equals(path) select c.CategoryServiceID).Single();
            return id;
        }
    }
}
